home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C18 / Cppcheck.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  4.4 KB  |  138 lines

  1. //: C18:Cppcheck.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Configures .h & .cpp files
  7. // To conform to style standard.
  8. // Tests existing files for conformance
  9. #include "../require.h"
  10. #include <fstream>
  11. #include <strstream>
  12. #include <cstring>
  13. #include <cctype>
  14. using namespace std;
  15.  
  16. int main(int argc, char* argv[]) {
  17.   const int sz = 40;  // Buffer sizes
  18.   const int bsz = 100;
  19.   requireArgs(argc, 1); // File set name
  20.   enum bufs { base, header, implement,
  21.     Hline1, guard1, guard2, guard3,
  22.     CPPline1, include, bufnum };
  23.   char b[bufnum][sz];
  24.   ostrstream osarray[] = {
  25.     ostrstream(b[base], sz),
  26.     ostrstream(b[header], sz),
  27.     ostrstream(b[implement], sz),
  28.     ostrstream(b[Hline1], sz),
  29.     ostrstream(b[guard1], sz),
  30.     ostrstream(b[guard2], sz),
  31.     ostrstream(b[guard3], sz),
  32.     ostrstream(b[CPPline1], sz),
  33.     ostrstream(b[include], sz),
  34.   };
  35.   osarray[base] << argv[1] << ends;
  36.   // Find any '.' in the string using the
  37.   // Standard C library function strchr():
  38.   char* period = strchr(b[base], '.');
  39.   if(period) *period = 0; // Strip extension
  40.   // Force to upper case:
  41.   for(int i = 0; b[base][i]; i++)
  42.     b[base][i] = toupper(b[base][i]);
  43.   // Create file names and internal lines:
  44.   osarray[header] << b[base] << ".h" << ends;
  45.   osarray[implement] << b[base] << ".cpp" << ends;
  46.   osarray[Hline1] << "//" << ": " << b[header]
  47.     << " -- " << ends;
  48.   osarray[guard1] << "#ifndef " << b[base]
  49.                   << "_H" << ends;
  50.   osarray[guard2] << "#define " << b[base]
  51.                   << "_H" << ends;
  52.   osarray[guard3] << "#endif // " << b[base]
  53.                   << "_H" << ends;
  54.   osarray[CPPline1] << "//" << ": "
  55.                     << b[implement]
  56.                     << " -- " << ends;
  57.   osarray[include] << "#include \""
  58.                    << b[header] << "\"" <<ends;
  59.   // First, try to open existing files:
  60.   ifstream existh(b[header]),
  61.            existcpp(b[implement]);
  62.   if(!existh) { // Doesn't exist; create it
  63.     ofstream newheader(b[header]);
  64.     assure(newheader, b[header]);
  65.     newheader << b[Hline1] << endl
  66.       << b[guard1] << endl
  67.       << b[guard2] << endl << endl
  68.       << b[guard3] << endl;
  69.   }
  70.   if(!existcpp) { // Create cpp file
  71.     ofstream newcpp(b[implement]);
  72.     assure(newcpp, b[implement]);
  73.     newcpp << b[CPPline1] << endl
  74.       << b[include] << endl;
  75.   }
  76.   if(existh) { // Already exists; verify it
  77.     strstream hfile; // Write & read
  78.     ostrstream newheader; // Write
  79.     hfile << existh.rdbuf() << ends;
  80.     // Check that first line conforms:
  81.     char buf[bsz];
  82.     if(hfile.getline(buf, bsz)) {
  83.       if(!strstr(buf, "//" ":") ||
  84.          !strstr(buf, b[header]))
  85.         newheader << b[Hline1] << endl;
  86.     }
  87.     // Ensure guard lines are in header:
  88.     if(!strstr(hfile.str(), b[guard1]) ||
  89.        !strstr(hfile.str(), b[guard2]) ||
  90.        !strstr(hfile.str(), b[guard3])) {
  91.        newheader << b[guard1] << endl
  92.          << b[guard2] << endl
  93.          << buf
  94.          << hfile.rdbuf() << endl
  95.          << b[guard3] << endl << ends;
  96.     } else
  97.       newheader << buf
  98.         << hfile.rdbuf() << ends;
  99.     // If there were changes, overwrite file:
  100.     if(strcmp(hfile.str(),newheader.str())!=0){
  101.       existh.close();
  102.       ofstream newH(b[header]);
  103.       assure(newH, b[header]);
  104.       newH << "//@//" << endl // Change marker
  105.         << newheader.rdbuf();
  106.     }
  107.     delete hfile.str();
  108.     delete newheader.str();
  109.   }
  110.   if(existcpp) { // Already exists; verify it
  111.     strstream cppfile;
  112.     ostrstream newcpp;
  113.     cppfile << existcpp.rdbuf() << ends;
  114.     char buf[bsz];
  115.     // Check that first line conforms:
  116.     if(cppfile.getline(buf, bsz))
  117.       if(!strstr(buf, "//" ":") ||
  118.          !strstr(buf, b[implement]))
  119.         newcpp << b[CPPline1] << endl;
  120.     // Ensure header is included:
  121.     if(!strstr(cppfile.str(), b[include]))
  122.       newcpp << b[include] << endl;
  123.     // Put in the rest of the file:
  124.     newcpp << buf << endl; // First line read
  125.     newcpp << cppfile.rdbuf() << ends;
  126.     // If there were changes, overwrite file:
  127.     if(strcmp(cppfile.str(),newcpp.str())!=0){
  128.       existcpp.close();
  129.       ofstream newCPP(b[implement]);
  130.       assure(newCPP, b[implement]);
  131.       newCPP << "//@//" << endl // Change marker
  132.         << newcpp.rdbuf();
  133.     }
  134.     delete cppfile.str();
  135.     delete newcpp.str();
  136.   }
  137. } ///:~
  138.